﻿using ch06.binary.module;

namespace ch06.binary.wasm_reader
{
    public partial struct WasmReader
    {
        // export sec
        private Export[] ReadExportSec()
        {
            var vec = new Export[ReadVarU32()];
            for (var i = 0; i < vec.Length; i++)
            {
                vec[i] = ReadExport();
            }

            return vec;
        }

        private Export ReadExport()
        {
            return new()
            {
                Name = ReadName(),
                Desc = ReadExportDesc(),
            };
        }

        private ExportDesc ReadExportDesc()
        {
            var desc = new ExportDesc
            {
                Tag = ReadByte(),
                Idx = ReadVarU32(),
            };
            switch (desc.Tag)
            {
                case ConstData.ExportTagFunc: // func_idx
                case ConstData.ExportTagTable: // table_idx
                case ConstData.ExportTagMem: // mem_idx
                case ConstData.ExportTagGlobal: // global_idx
                    break;
                default:
                    Panic.Run("invalid export desc tag: %d", desc.Tag);
                    break;
            }

            return desc;
        }
    }
}